<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function getImg() {
      return new Promise((reslove, reject) => {
        const oImg = new Image();
        oImg.onload = function () {
          reslove(oImg)
        }
        oImg.src = 'https://cdn.eso.org/images.screen/eso1242a.jpg'
      })
    }

    function timeout() {
      return new Promise((reslove, reject) => {
        setTimeout(() => {
          reject('图片请求超时!!!!')
        }, 3000)
      })
    }

    Promise.race([
        getImg(),
        timeout()
      ])
      .then((res) => {
        console.log(res);
      })
      .catch((err) => {
        console.log(err);
      })
  </script>
</body>

</html>